package com.coconet.gas.quartz;

import com.coconet.gas.constant.dwEnum;
import com.coconet.gas.dao.ScanQuartzMapper;
import com.coconet.gas.exception.ServiceException;
import com.coconet.gas.service.TaskService;
import com.coconet.gas.service.impl.OpenvasTool2;
import com.coconet.gas.vo.dw.ScanQuartz;
import com.gxa.entity.TaskInfoEntity;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;


@Component
public class ScanQuartzJob implements Job {
    @Autowired
    private Scheduler scheduler;
    @Autowired
    private ScanQuartzMapper sqMapper;
    @Autowired
    private TaskService taskService;
    @Autowired
    private OpenvasTool2 openvasTool;

    public void run(ScanQuartz sq) {
        //String taskid = "scan-Quartz";
        String groupid = "scan-Quartz";
        JobKey jobKey = new JobKey(sq.getTaskId(), groupid);
        JobDetail jobDetail;
        try {
            jobDetail = scheduler.getJobDetail(jobKey);
            if (jobDetail == null) {
                //ScanQuartz sq = sqMapper.selectByPrimaryKey(sq.getTaskId());
                jobDetail = JobBuilder.newJob(ScanQuartzJob.class).withIdentity(sq.getTaskId(), groupid).build();
                jobDetail.getJobDataMap().put("taskId", sq.getTaskId());
                CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(sq.getCron());
                // CronTrigger表达式触发器 继承于Trigger
                // TriggerBuilder 用于构建触发器实例
                CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity(sq.getTaskId(), groupid)
                        .withSchedule(cronScheduleBuilder).build();
                scheduler.scheduleJob(jobDetail, cronTrigger);
                sq.setOnOff(1);
                sqMapper.updateByPrimaryKeySelective(sq);
            }
        } catch (SchedulerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void stop(String taskId) {
        //String taskid = "scan-Quartz";
        String groupid = "scan-Quartz";
        JobKey jobKey = new JobKey(taskId, groupid);
        JobDetail jobDetail;
        try {
            jobDetail = scheduler.getJobDetail(jobKey);
            if (jobDetail != null) {
                scheduler.deleteJob(jobKey);
            }
            ScanQuartz sq = new ScanQuartz();
            sq.setOnOff(0);
            sq.setTaskId(taskId);
            sqMapper.updateByPrimaryKeySelective(sq);
        } catch (SchedulerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void update(ScanQuartz sq) {
        //String taskid = "scan-Quartz";
        String groupid = "scan-Quartz";
        TriggerKey triggerKey = new TriggerKey(sq.getTaskId(), groupid);
        CronTrigger cronTrigger;
        try {
            cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            if (cronTrigger == null) {
                throw new ServiceException(dwEnum.FAILEDTASKS.code(), "任务已失效，请重新运行在进行修改");
            }
            String oldTime = cronTrigger.getCronExpression();
            if (!oldTime.equalsIgnoreCase(sq.getCron())) {
                CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(sq.getCron());
                CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(sq.getTaskId(), groupid)
                        .withSchedule(cronScheduleBuilder).build();
                scheduler.rescheduleJob(triggerKey, trigger);
                sqMapper.updateByPrimaryKeySelective(sq);
            }
        } catch (SchedulerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        String taskId = (String) context.getJobDetail().getJobDataMap().get("taskId");
        try {
            taskService.startTask(taskId);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
